package HW4;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class CheckServer {

            public static void main(String[] args)  {
                System.out.println("服务器接收数据！");
                //连接客户端利用Socket创建输入流并接收数据
                ServerSocket ss=null;
                Socket s =null;
                ObjectInputStream ois = null;
                //UserMessage um = null;
                ArrayList<UserMessage> al = null;

                OutputStream os = null;
                DataOutputStream dos = null;

                try {

                    ss = new ServerSocket(8888);
                    s = ss.accept();
                    //接收客户端信息
                    ois = new ObjectInputStream(s.getInputStream());
                    //System.out.println("打印" + ois.readObject());
                    al = (ArrayList<UserMessage>)ois.readObject();
                    //um =(ArrayList)ois.readObject();

                    //关闭输入流
                    //s.shutdownInput();
                    for(UserMessage um: al){
                        System.out.println(um.getUser().getName());

                    }
                    //发送信息给客户端
                    os = s.getOutputStream();
                    dos = new DataOutputStream(os);

                    //对接收到到数据判定
                     for(UserMessage um: al){

                         if("admin".equals(um.getUser().getName())&&"123123".equals(um.getUser().getPassWord())) {
                             dos.writeUTF("登陆成功！");
                         }else {
                             dos.writeUTF("登陆失败！");
                         }
                     }

                } catch(IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }finally{
                    try {
                        s.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        dos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }


}
